package com.erb.service.wzq.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.erb.config.AlipayConfig;
import com.erb.service.wzq.AlipayService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Created by MoYao on 2020/7/6.
 */
@Service
public class AlipayServiceImpl implements AlipayService {
	@Override
	public void aliPay(HttpServletResponse response, double tradeMoney) throws IOException {
		response.setContentType("text/html;charset=utf-8");

		PrintWriter out = response.getWriter();
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		//设置请求参数
		AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
		aliPayRequest.setReturnUrl(AlipayConfig.return_url);
//        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);

		//商户订单号，后台可以写一个工具类生成一个订单号，必填
		Integer order_number = new Integer((int) new Date().getTime());
		//付款金额，从前台获取，必填
		double total_amount = new Double(tradeMoney);
		//订单名称，必填
		String subject = "支付宝沙箱支付充值到余额";
		aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
				+ "\"total_amount\":\"" + total_amount + "\","
				+ "\"subject\":\"" + subject + "\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		//请求
		String result = null;
		try {
			result = alipayClient.pageExecute(aliPayRequest).getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		//输出
		out.println(result);
	}
}
